<template>
	<view class="userInfo">
		<view class="user_list">
			<view class="user_list_meun">
				<text class="user_list_text">头像</text>
				<view style="display: flex;">
					<image :src="userInfo.index_avatar" class="userInfo_img"></image>
					<u-icon name="arrow-right" color="#dddddd" size="16" @click="ChooseImage"></u-icon>
				</view>
			</view>
			<view class="user_list_meun" @click="toPage(1)">
				<text class="user_list_text">昵称</text>
				<view class="user_list_title">{{userInfo.index_name ? userInfo.index_name : '微信用户'}} <u-icon name="arrow-right" color="#dddddd" size="16" style="margin-left: 15rpx;"></u-icon></view>
			</view>
			<view class="user_list_meun" @click="toPage(2)">
				<text class="user_list_text">简介</text>
				<u-icon name="arrow-right" color="#dddddd" size="16"></u-icon>
			</view>
			<view class="user_list_meun" @click="toPage(3)">
				<text class="user_list_text">联系方式</text>
				<u-icon name="arrow-right" color="#dddddd" size="16"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import {userIndexAPI,userUpdateAPI} from '@/api/user'
	export default {
		data() {
			return {
				userInfo:{},
			}
		},
		mounted() {
			
		},
		onShow() {
			this.getData()
		},
		methods:{
			getData(){
				uni.showLoading({
					title:'加载中'
				})
				var parems = {
					token:uni.getStorageSync('accessToken'),
					id:uni.getStorageSync('userInfo').id
				}
				userIndexAPI(parems).then(res =>{
					if(res.code == 1){
						this.userInfo = res.data.user
					}else{
						wx.showToast({
							title:res.msg,
							icon:'none'
						})
					}
					uni.hideLoading()
				})
			},
			//上传图片
			ChooseImage() {
				let self = this;
				uni.chooseImage({
					count: 1, //可选择数量，默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或从使用相机
					success: async (res) => {
						//上传图片大小限制
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							if (res.tempFiles[0].size > 2 * 1024 * 1000) {  //上传图片大小限制
								uni.showToast({
									title: "照片大小不能超过2MB",
									icon: "none"
								});
								return
							}
						}
						try {
							uni.showLoading({
								title: "上传中...",
								mask: true
							});
							uni.uploadFile({
								url: 'https://qjd.511lh.cn/addons/qiniu/index/upload', //post请求的地址
								filePath: res.tempFilePaths[0],
								name: 'file',
								formData: {
									'qiniutoken': uni.getStorageSync('accessToken')  //formData是指除了图片以外，额外加的字段
								},
								success: (uploadFileRes) => {
									console.log(uploadFileRes.data)
									//这里要注意，uploadFileRes.data是个String类型，要转对象的话需要JSON.parse一下
									let obj = JSON.parse(uploadFileRes.data);
									self.userInfo.index_avatar = obj.data.fullurl;
									setTimeout(() => {
										this.upData(obj.data.fullurl)
									}, 1000)
								},
								fail: (res) => {
									console.log(res);
								}
							})
						} catch (e) {
							console.log(e);
						} finally {
							uni.hideLoading();
						}
					}
				});
			},
			upData(data){
				var parems = {
					token:uni.getStorageSync('accessToken'),
					index_avatar:data
				}
				userUpdateAPI(parems).then(res =>{
					if(res.code == 1){
						this.getData()
					}else{
						wx.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			//查看图片
			ViewImage(e) {
				let urls = [];
				for (let i = 0; i < this.imgList.length; i++) {
					urls.push(this.imgList[i].url);
				}
				uni.previewImage({
					urls: urls,
					current: e.currentTarget.dataset.url
				});
			},
			//删除图片
			DelImg(e) {
				let self = this;
				uni.showModal({
					title: '',
					content: '确定要删除么',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							let index = e.currentTarget.dataset.index;
							if (self.imgList.length != 0) {
								let id = self.imgList[index].id;
								let url = self.$serverPath.UF + 'file/delete/' + id;
								this.$util.urlRequest(url, {}, 'POST', function (ret) {
									let res = ret.data;
									if (res.code == 0) {
										self.imgList.splice(index, 1);
									} else {
										uni.showToast({
											title: "删除失败，请重试",
											icon: "none"
										});
										console.log('请求失败:', res)
									}
								});
							}

						}
					}
				})
			},
			toPage(data){
				uni.navigateTo({
					url:'./editUserInfo?type=' + data
				})
			}
		}
		
	}
</script>

<style lang="scss">
	page{
		background: RGBA(249, 249, 249, 0.75) !important;
	}
	.userInfo{
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		.userInfo_header{
			display: flex;
			align-items: center;
			width: 100%;
			padding-top: 30rpx;
			padding-bottom: 30rpx;
			background: #fff;
		}
		.userInfo_img{
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background: #c0c4cc;
			margin-right: 15rpx;
		}
		.userInfo_name{
			font-size: 40rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #0A0A0A;
			margin-bottom: 20rpx;
		}
		.userInfo_right{
			display: flex;
			align-items: center;
		}
		.userInfo_text{
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #F8DE54;
			margin-left: 5rpx;
		}
		.user_list{
			width: 100%;
			background: #fff;
			margin-top: 24rpx;
		}
		.user_list_meun{
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 100rpx;
			border-bottom: 2rpx solid #eee;
			padding: 0 30rpx;
		}
		.user_list_text{
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #918F8F;
		}
		.user_list_title{
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #3A3E41;
			display: flex;
			align-items: center;
		}
	}
</style>